package com.example.polarity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class MagnetGUI extends View
{
	public Bitmap MagnetImage;
	public int magnetType;
	
	public int width;
	public int height;
	
	public boolean start = false;
	
	
	public boolean mouseDown = false;
	
	MainActivity mainActivity; 
	
	public Canvas MagnetCanvas; 
	public MagnetGUI(Context context)
	{
		super(context);
	}
	
	public MagnetGUI(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		this.setWillNotDraw(false);
		if(this.getTag().equals("Neg"))
			magnetType = 1;
		else	
			magnetType = 0;
		
		if(!isInEditMode())
			mainActivity = (MainActivity)context;
		
	}
	
	public MagnetGUI(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		
		if(mainActivity.StartPlayButton.getText().equals("Start"))
		{
			if(!mouseDown)
			{
				mainActivity.GameView.curMagnet = new Magnet(this.getContext(),this, this.magnetType);
			}
			
			if(event.getAction() == MotionEvent.ACTION_UP)
			{
	
				mainActivity.GameView.MagnetList.add(mainActivity.GameView.curMagnet);
				mouseDown = false;
				mainActivity.GameView.curMagnet = null;
			}
			else
			if(event.getAction() == MotionEvent.ACTION_MOVE)
			{
				mouseDown = true;
				mainActivity.GameView.curMagnet.DrawMagnet(MagnetCanvas,this.getLeft() - (-1 * event.getX()), this.getTop() - (-1 * event.getY())  );
			}
		}
		return true;
	}
}
